// GenerateComponent.cpp: implementation of the GenerateComponent class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GenerateComponent.h"
#include "ReadEntityDefinition.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

GenerateComponent::GenerateComponent()
{
	m_pComponent = NULL ;

}

GenerateComponent::~GenerateComponent()
{
	if( m_pComponent !=NULL )
	{
		delete m_pComponent;
	}
}

EDComponent* GenerateComponent::GenerateComponentFromTemplate(const EquipDataSourceManager &dsm)
{
	EDComponent* pTCpnt = EDCpntTypeID::IsCTIDRegistered( dsm.CurrentDataSourceItem()->Path(),
		dsm.CurrentDataSourceItem()->FileName(), dsm.CurrentDataSourceItem()->FormatName() );
	if ( pTCpnt == 0 ) return NULL;
	EDComponent* pCpnt = new EDComponent;
	if ( pCpnt->CopyFromTemplate( *pTCpnt ) == FALSE ) { delete pCpnt; return NULL; }
	if ( pCpnt->m_pCpntID == NULL )	pCpnt->m_pCpntID = new EDComponentID;
	pCpnt->m_pCpntID->m_strTable = dsm.CurrentDataSourceItem()->QueryStatement();
	pCpnt->m_pCpntID->m_pConn = dsm.CurrentDataSourceItem()->Connection();
	if ( !GenerateSubCpntFromTemplate( pCpnt, dsm ) ) { delete pCpnt; return NULL; }
	return pCpnt;

}

bool GenerateComponent::GenerateSubCpntFromTemplate(EDComponent *pNewCpnt, const EquipDataSourceManager &dsm)
{
	if ( pNewCpnt == 0 ) return false;
	if ( pNewCpnt->m_parEDComponent == 0 ) return true;

	for ( int i = 0; i < pNewCpnt->m_parEDComponent->GetSize(); i++ )
	{
		EDComponent* ptr = dynamic_cast<EDComponent*>( pNewCpnt->m_parEDComponent->GetAt( i ) );
		if ( ptr == 0 ) continue;
		const EquipDataSourceItem* dsi = dsm.GetDSItemByPFID( ptr->m_sem.m_strAbs );
		if ( dsi == 0 ) continue;
		EDCpntTypeID* pp = 0;
		pp = EDCpntTypeID::RegisterCTID( dsi->Path(), dsi->FileName(), dsi->FormatName() );
		if ( pp == 0 ) 
		{
			ReadEntityDefinition entity;
			if(!entity.ReadScript( dsi->Path(), dsi->FileName(), dsi->FormatName() ))
				continue;
			pp = EDCpntTypeID::RegisterCTID( dsi->Path(), dsi->FileName(), dsi->FormatName() );
			ASSERT(pp);
		}
		if ( ptr->CopyFromTemplate( *pp->Component() ) == FALSE ) continue;
		if ( ptr->m_pCpntID == NULL )
			ptr->m_pCpntID = new EDComponentID;
		
		ptr->m_pCpntID->m_strTable = dsi->QueryStatement();
		ptr->m_pCpntID->m_pConn = dsi->Connection();
		GenerateSubCpntFromTemplate( ptr, *(dsm.GetDSMByPFID( ptr->m_sem.m_strAbs ) ) );
	}
	return true;
}

AcDbEntity* GenerateComponent::CreateEntity(EquipDataSourceManager* pDsm)
{
	ReadEntityDefinition entity;
	if( !entity.ReadScript(	pDsm->CurrentDataSourceItem()->Path(), 
						pDsm->CurrentDataSourceItem()->FileName(), 
						pDsm->CurrentDataSourceItem()->FormatName()))
		return NULL;

	m_pComponent = GenerateComponentFromTemplate(*pDsm);
	if(m_pComponent == NULL) return NULL;

	return m_pComponent->MakeDbEntity( false ) ;

}

#include "RCSFlagGetter.h"
#include "RCSFlagGetStrategyForSH.h"
extern "C" __declspec(dllexport) AcDbEntity* CreateEntity( DataSourceManager* pdsm )
{
	RCSFlagGetter::Instance().SetGetter( &RCSFlagGetStrategyForSH::Instance() );

	GenerateComponent ComponentFactory;	
	return ComponentFactory.CreateEntity( pdsm) ;
}